package com.alibaba.mqtt.consumer.controller;

import com.alibaba.common.api.service.IMQTTPublisherService;
import com.alibaba.common.api.service.IMQTTSubscriberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

@Slf4j
@RestController
public class DemoRestController {
    public static String CLOUD_TOPIC = "mqtt/cloud";
    public static String CAR_TOPIC = "mqtt/car";

    @Reference(version = "1.0.0",group = "demo")
    IMQTTPublisherService publisher;

    @Reference(version = "1.0.0",group = "demo")
    IMQTTSubscriberService subscriber;

    @PostConstruct
    public void init() {
        subscriber.subscribeMessage(CAR_TOPIC);
    }

    @PostMapping("/mqtt/cloud/pub/message")
    public String pubMessage(@RequestBody String data) {
        publisher.publishMessage(CLOUD_TOPIC, data);
        return "Cloud success send " + data;
    }

    @PostMapping("/mqtt/cloud/pub/response")
    public String pubResponse(@RequestBody String response){
        System.out.println("Cloud get response success " + response);
        return "Cloud get response success " + response;
    }

}

